package il.ac.braude.sona.gui.forms.questions;

import il.ac.braude.sona.constants.Resources;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;

import com.ofekron.genericlist.List;
import com.ofekron.genericlist.standart.ListStandartController;
import com.ofekron.genericlist.standart.ListStandartModel;
/**
 * Implements file upload GUI
 * @author Ofek Ron
 * @author Avihay Avitan
 * @author Nitzan Inbar
 * @author Sasha Rozenthal
 * 
 */
@SuppressWarnings("serial")
public class QFileChooser extends FormQuestion implements ActionListener {
	private JFileChooser fileChooser = new JFileChooser();
	private ListStandartModel<File> listFileModel = new ListStandartModel<File>();
	private List<File> listFiles = new List<File>(listFileModel,new ListStandartController<File>());
	/**
	 * Makes file choice visible.
	 * @param question 
	 */
	public QFileChooser(String question) {
		super(BoxLayout.Y_AXIS);
		JButton browse = new JButton(Resources.Strings.Browse);
		browse.addActionListener(this);
		JPanel control = new JPanel();
		control.add(new JLabel(question));
		control.add(browse);
		JPanel view = new JPanel(new BorderLayout());
		view.add(control, BorderLayout.NORTH);
		view.add(listFiles);
		add(view);
	}
	@Override
	public void setDefaultAnswer(Object defaultAnswer) {
	}

	@Override
	public Object getAnswer() {
		return listFiles.getListDataModel().getAll();
	}
	@Override
	public void actionPerformed(ActionEvent arg0) {
		   int returnVal = fileChooser.showOpenDialog(this);
	        if (returnVal == JFileChooser.APPROVE_OPTION) {
	            File file = fileChooser.getSelectedFile();
	            listFileModel.add(file);
	        }
	}
}
